<template>
  <LoginHeader>联合登录</LoginHeader>
  <!--  加载出现-->
  <section class="container" v-if="loading">
    <div class="unbind">
      <div class="loading"></div>
    </div>
  </section>

  <!--    加载完成出现-->
  <section class="container" v-if="!loading && !isBind">
    <nav class="tab">
      <a @click="hasAccount = true" :class="{ active: hasAccount }">
        <i class="iconfont icon-bind"></i>
        <span>已有小兔鲜账号，请绑定手机</span>
      </a>
      <a @click="hasAccount = false" :class="{ active: !hasAccount }">
        <i class="iconfont icon-edit"></i>
        <span>没有小兔鲜账号，请完善资料</span>
      </a>
    </nav>
    <div class="tab-content" v-if="hasAccount">
      <LoginCallbackBindPhone :unionId="unionId" />
    </div>
    <div class="tab-content" v-if="!hasAccount">
      <LoginCallbackBindPatch :unionId="unionId" />
    </div>
  </section>
  <LoginFooter />
</template>

<script>
import LoginHeader from "@/views/login/components/LoginHeader";
import LoginFooter from "@/views/login/components/LoginFooter";
import LoginCallbackBindPhone from "@/views/login/components/LoginCallbackBindPhone";
import LoginCallbackBindPatch from "@/views/login/components/LoginCallbackBindPatch";
import { ref } from "vue";
import useLoginAfter from "../../hooks/useLoginAfter";
import { findAccountByQQOpenid } from "../../api/user";

export default {
  name: "LoginCallbackPage",
  components: {
    LoginFooter,
    LoginHeader,
    LoginCallbackBindPatch,
    LoginCallbackBindPhone,
  },
  setup() {
    const hasAccount = ref(true);

    // 1.假设QQ用户没有绑定小兔仙账号
    const isBind = ref(false);
    // 2.loading状态
    const loading = ref(false);
    // 3.储存openid
    const unionId = ref("");
    // 登录后储存用户信息
    const { loginSuccess } = useLoginAfter();
    // 获取QQ联合中和登录相关的API
    const Login = window.QC.Login;
    // 检测地址栏中是否存在 access_token
    if (Login.check()) {
      // 1. 获取地址栏中的 access_token 值
      // 2. 向QQ互联服务器发送请求使用 access_token 换取用户的唯一标识 openid
      // https://graph.qq.com/oauth2.0/me?access_token=B204C56879FC0C2818AF844746BD6959

      // 加载中
      loading.value = true;
      // 获取用户唯一标识
      Login.getMe((openid) => {
        // 储存openid
        unionId.value = openid;
        // 检测QQ绑定账户
        findAccountByQQOpenid({ unionId: openid })
          .then(
            // 检测到账号 走then回调函数 执行登录成功后的逻辑
            (data) => {
              // 更新加载状态
              loading.value = false;
              // 更新绑定状态
              isBind.value = true;
              loginSuccess(data);
            }
          )
          .catch(() => {
            // 更新加载状态
            loading.value = false;
            // 更新绑定状态
            isBind.value = false;
          });
      });
    }

    return { hasAccount, isBind, loading, unionId };
  },
};
</script>

<style scoped lang="less">
.container {
  padding: 25px 0;
  position: relative;
  height: 730px;
  .unbind {
    position: absolute;
    left: 0;
    top: 0;
    width: 100%;
    height: 100%;
    padding: 25px 0;
    z-index: 99;
    .loading {
      height: 100%;
      background: #fff url(../../assets/images/load.gif) no-repeat center /
        100px 100px;
    }
  }
}
.tab {
  background: #fff;
  height: 80px;
  padding-top: 40px;
  font-size: 18px;
  text-align: center;
  a {
    color: #666;
    display: inline-block;
    width: 350px;
    line-height: 40px;
    border-bottom: 2px solid #e4e4e4;
    i {
      font-size: 22px;
      vertical-align: middle;
    }
    span {
      vertical-align: middle;
      margin-left: 4px;
    }
    &.active {
      color: @xtxColor;
      border-color: @xtxColor;
    }
  }
}
.tab-content {
  min-height: 600px;
  background: #fff;
}
</style>
